﻿using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using SCM.API.Read.Application.Command.Ledger;
using SCM.ErrorCode;
using SCM.Extensions.Ledger;
using SCM.Infrastructure.Interface;
using System.Text;

namespace SCM.API.Read.Application.Handler.Ledger
{
    public class GetWarnHandler : IRequestHandler<GetWarnCommand, APIPaging<WarnDTO>>
    {
        IDatabaseContext db;

        public GetWarnHandler(IDatabaseContext db)
        {
            this.db = db;
        }

        public async Task<APIPaging<WarnDTO>> Handle(GetWarnCommand request, CancellationToken cancellationToken)
        {
            var result = new APIPaging<WarnDTO>();

            var countSql = new StringBuilder();
            var sqlBuilder = new StringBuilder();

            sqlBuilder.Append(@"SELECT  a.id,
                                        g.ProductId,
                                        g.ProductCode,
                                        g.ProductName,
                                        b.CategoryId,
                                        b.CategoryName,
                                        g.Specifications,
                                        a.Stocknum,
                                        a.Upper,
                                        a.Lower,
                                        a.currentStock,
                                        a.isDel
                                FROM WarnModel a JOIN CategoryModel b on a.CategoryId=b.CategoryId 
                                                  JOIN ProductModel g on a.ProductId=g.ProductId 
                                WHERE a.IsDel=0 AND b.IsDel=0  AND g.IsDel=0 ");

            countSql.Append(@"SELECT COUNT(a.id)  FROM WarnModel a JOIN CategoryModel b on a.CategoryId=b.CategoryId 
                                                  JOIN ProductModel g on a.ProductId=g.ProductId 
                                WHERE a.IsDel=0 AND b.IsDel=0  AND g.IsDel=0  ");
            Console.WriteLine(sqlBuilder.ToString());
            var parameters = new DynamicParameters();

            if (!string.IsNullOrEmpty(request.ProductName))
            {
                sqlBuilder.Append(" AND a.ProductName LIKE @a.ProductName ");
                parameters.Add("a.ProductName", $"%{request.ProductName}%");
                countSql.Append(" AND a.ProductName LIKE @a.ProductName ");
            }
            if (request.Upper != null)
            {
                sqlBuilder.Append("AND Upper = @Upper ");
                parameters.Add("Upper", request.Upper);
                countSql.Append("AND Upper = @Upper ");
            }
            if (request.Lower != null)
            {
                sqlBuilder.Append("AND Lower = @Lower ");
                parameters.Add("Lower", request.Lower);
                countSql.Append("AND Lower = @Lower ");
            }


            sqlBuilder.Append("ORDER BY id DESC ");
            sqlBuilder.Append("LIMIT @PageSize OFFSET @Offset ");
            parameters.Add("PageSize", request.PageSize);
            parameters.Add("Offset", (request.PageIndex - 1) * request.PageSize);

           

            var data = await db.DapperConnection.QueryAsync<WarnDTO>(sqlBuilder.ToString(), parameters);
            var total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);

            result.Msg = "显示成功";
            result.Code = APIEnum.Success;
            result.items = data.ToList();
            result.totalCount = total;
            result.totalPage = (int)Math.Ceiling((double)total / request.PageSize);
            return result;

        }
    }
}
